package com.useeinfo.oa.modules.maintaining.biz;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.FacilityBiz;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Facility;
import com.useeinfo.oa.modules.maintaining.dao.FacilityMaintainSnapDao;
import com.useeinfo.oa.modules.maintaining.entity.*;
import com.useeinfo.oa.modules.maintaining.enums.InputTypeEnum;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @Date: 2018/12/28 13:00
 */
@Service("facilityMaintainSnapBiz")
public class FacilityMaintainSnapBiz extends CrudBiz<FacilityMaintainSnapDao, FacilityMaintainSnap> {
    @Autowired
    private MaintainOrderBiz maintainOrderBiz;
    @Autowired
    private FacilityBiz facilityBiz;
    @Autowired
    private MaintainTechnologyBiz maintainTechnologyBiz;
    @Autowired
    private MaintainOrderWorkContentBiz maintainOrderWorkContentBiz;

    /**
     * 保存设备数据
     * @param facilityIds
     * @param serial
     * @param request
     * @return
     */
    public ResultDto saveFacility(String facilityIds, String maintainOrderId, String serial, HttpServletRequest request){

        MaintainOrder maintainOrder = maintainOrderBiz.findModel(StringConverters.null2Long(maintainOrderId));
        net.sf.json.JSONArray jsonArray = net.sf.json.JSONArray.fromObject(facilityIds);
        for (Object id : jsonArray) {
            FacilityMaintainSnap facilityMaintainSnap;
            Facility facility = facilityBiz.findModel(StringConverters.null2Long(id));
            List<FacilityMaintainSnap> facilityMaintainSnapList = dao.findByFacilityId(StringConverters.null2Long(id), serial);
            if (CollectionUtils.isNotEmpty(facilityMaintainSnapList)) {
                facilityMaintainSnap = facilityMaintainSnapList.get(0);
            } else {
                facilityMaintainSnap = new FacilityMaintainSnap();
                facilityMaintainSnap.setTempVal(Boolean.TRUE);
            }
            facilityMaintainSnap.setMaintainOrder(maintainOrder);
            facilityMaintainSnap.setSerial(serial);
            facilityMaintainSnap.setSpec(facility.getType());
            facilityMaintainSnap.setFacilityId(StringConverters.null2Long(id));
            facilityMaintainSnap.setFacilityName(facility.getName());
            facilityMaintainSnap.setFacilityCode(facility.getCode());
            facilityMaintainSnap.setFacilityGroupId(facility.getFacilityGroup() != null ? facility.getFacilityGroup().getId() : null);
            facilityMaintainSnap.setFacilityGroupName(facility.getFacilityGroup() != null ? facility.getFacilityGroup().getFacilityGroupName() : "");
            facilityMaintainSnap.setAreaId(facility.getArea() != null ? facility.getArea().getId() : null);
            facilityMaintainSnap.setAreaName(facility.getArea() != null ? facility.getArea().getAreaName() : "");
            facilityMaintainSnap.setFacilityCatId(facility.getFacilityCat() != null ? facility.getFacilityCat().getId() : null);
            facilityMaintainSnap.setFacilityCatName(facility.getFacilityCat() != null ? facility.getFacilityCat().getCatName() : "");
            if(facilityMaintainSnap.getId()!=null){
                dao.add(facilityMaintainSnap);
            }else{
                dao.update(facilityMaintainSnap);
            }
        }
        return ResultUtil.success();
    }

    /**
     * 保存工艺数据
     * @param facilityMaintainSnapIds
     * @param maintainId
     * @param maintainOrderId
     * @return
     */
    public ResultDto saveMaintain(String facilityMaintainSnapIds, String maintainId, String maintainOrderId){
        MaintainOrder maintainOrder = maintainOrderBiz.findModel(StringConverters.null2Long(maintainOrderId));
        MaintainTechnology maintainTechnology = maintainTechnologyBiz.findModel(StringConverters.null2Long(maintainId));
        for(String id:facilityMaintainSnapIds.split(",")){
            FacilityMaintainSnap facilityMaintainSnap = dao.findModel(StringConverters.null2Long(id));
            facilityMaintainSnap.setMaintainOrder(maintainOrder);
            facilityMaintainSnap.setMaintainId(maintainTechnology.getId());
            facilityMaintainSnap.setMaintainName(maintainTechnology.getName());
            facilityMaintainSnap.setSafety(maintainTechnology.getSafetyRequirement());
            dao.update(facilityMaintainSnap);
        }
        return ResultUtil.success();

    }

    /**
     * app接口保存设备工艺数据
     * @param maintainOrderId
     * @param facilityId
     * @param maintainId
     * @param serial
     */
    public FacilityMaintainSnap saveFacilityMaintainSnap(String maintainOrderId, String facilityId, String maintainId, String serial){

        MaintainOrder maintainOrder = maintainOrderBiz.findModel(StringConverters.null2Long(maintainOrderId));
        FacilityMaintainSnap facilityMaintainSnap=new FacilityMaintainSnap();
        Facility facility = facilityBiz.findModel(StringConverters.null2Long(facilityId));
        if(facility==null){
            return null;
        }
        facilityMaintainSnap.setSerial(serial);
        facilityMaintainSnap.setMaintainOrder(maintainOrder);
        facilityMaintainSnap.setSerial(maintainOrder.getSerial());
        facilityMaintainSnap.setFacilityId(StringConverters.null2Long(facilityId));
        facilityMaintainSnap.setFacilityName(facility.getName());
        facilityMaintainSnap.setFacilityCode(facility.getCode());
        facilityMaintainSnap.setFacilityGroupId(facility.getFacilityGroup() != null ? facility.getFacilityGroup().getId() : null);
        facilityMaintainSnap.setFacilityGroupName(facility.getFacilityGroup() != null ? facility.getFacilityGroup().getFacilityGroupName() : "");
        facilityMaintainSnap.setAreaId(facility.getArea() != null ? facility.getArea().getId() : null);
        facilityMaintainSnap.setAreaName(facility.getArea() != null ? facility.getArea().getAreaName() : "");
        facilityMaintainSnap.setFacilityCatId(facility.getFacilityCat() != null ? facility.getFacilityCat().getId() : null);
        facilityMaintainSnap.setFacilityCatName(facility.getFacilityCat() != null ? facility.getFacilityCat().getCatName() : "");
        facilityMaintainSnap.setSpec(facility.getType());
        //保存工艺数据
        MaintainTechnology maintainTechnology = maintainTechnologyBiz.findModel(StringConverters.null2Long(maintainId));
        if(maintainTechnology==null){
            return null;
        }
        facilityMaintainSnap.setMaintainId(maintainTechnology.getId());
        facilityMaintainSnap.setMaintainName(maintainTechnology.getName());
        facilityMaintainSnap.setSafety(maintainTechnology.getSafetyRequirement());
        dao.add(facilityMaintainSnap);


        /**解析工艺内容start***/
        JSONArray jsonArray = JSONArray.parseArray(maintainTechnology.getMaintainDataContent());
        for (Object object : jsonArray) {
            JSONObject jsonObject = (JSONObject) object;
            MaintainOrderWorkContent maintainOrderWorkContent = new MaintainOrderWorkContent();
            maintainOrderWorkContent.setCreateDate(new Date());
            maintainOrderWorkContent.setFacilityMaintainSnap(facilityMaintainSnap);
            maintainOrderWorkContent.setName(jsonObject.getString("name"));
            maintainOrderWorkContent.setMaxNumber(jsonObject.getDouble("maxNumber"));
            maintainOrderWorkContent.setMinNumber(jsonObject.getDouble("minNumber"));
            maintainOrderWorkContent.setPhoto(jsonObject.getBoolean("photo"));
            maintainOrderWorkContent.setApplication_scenarios_id(jsonObject.getLong("applicationScenariosId"));
            maintainOrderWorkContent.setApplication_scenarios_name(jsonObject.getString("applicationScenariosName"));
            maintainOrderWorkContent.setContent(jsonObject.getString("content"));
            maintainOrderWorkContent.setResourceIds(jsonObject.getString("resourceIds"));
            maintainOrderWorkContent.setUnit(jsonObject.getString("unit"));
            maintainOrderWorkContent.setCategory(jsonObject.getString("category"));
            maintainOrderWorkContent.setInputTypeEnum(InputTypeEnum.valueOf(jsonObject.getString("inputTypeEnumCode")));
            maintainOrderWorkContentBiz.addOrUpdate(maintainOrderWorkContent);
        }
        /**解析工艺内容end***/

        return facilityMaintainSnap;

    }


}
